﻿using AF_Application.Interface.Cache;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AF_Web;
using System.Configuration;
using AF_Application.Interface.Log;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using AF_Cache;

namespace AF_CouchebaseLog
{
    /// <summary>
    /// 此类在IOC中以单例模式加载。
    /// </summary>
    public class CouchbaseLogProvider : ILogProvider
    {
        private uint LogCacheSeconds = 300;
        private string LogBucketName;
        private string LogBucketPassword;
        private string AppName;
        private string LogType = "file";
        private int LogLeve = 3;
        private string logFile = "";
        private readonly static object lockObj = new object();
        private ICacheProvider CacheProvider;

        public CouchbaseLogProvider(ICacheProvider cacheProvider)
        {
            CacheProvider = cacheProvider;
            logFile = Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("/bin/AF_CouchebaseLog.DLL", "/Logs/Debug.log");

            AppName = ConfigurationManager.AppSettings.GetString("AppName");
            if (string.IsNullOrEmpty(AppName))
            {
                throw new Exception("请在Config文件AppSetting中配置AppName项");
            }
            LogType = ConfigurationManager.AppSettings.GetString("LogType");
            LogCacheSeconds = (uint)ConfigurationManager.AppSettings.GetInt("LogCacheSeconds");
            LogBucketName = ConfigurationManager.AppSettings.GetString("LogBucketName");
            LogBucketPassword = ConfigurationManager.AppSettings.GetString("LogBucketPassword");
            LogLeve = ConfigurationManager.AppSettings.GetInt("LogLeve");
        }

        public void Dispose()
        {
        }

        public void WriteLog(EnumLogType logType, string message)
        {
            lock (lockObj)
            {
                if (LogLeve <= (int)logType)
                {
                    if (LogType == "cache")
                    {
                        LogEntity Log = new LogEntity()
                        {
                            AppName = AppName,
                            LogText = message,
                            LogTime = DateTime.Now,
                            LogType = (int)logType
                        };
                        CacheProvider.Set<LogEntity>(Guid.NewGuid().ToString(), Log, LogCacheSeconds, LogBucketName, LogBucketPassword);
                    }
                    else
                    {
                        File.AppendAllText(logFile, string.Format("{0} {1} {2}{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), logType.ToString(), message, Environment.NewLine));
                    }
                }
            }
        }

        public void WriteLog(EnumLogType logType, string message, Exception exp)
        {
            lock (lockObj)
            {
                if (LogLeve <= (int)logType)
                {
                    if (LogType == "cache")
                    {
                        LogEntity Log = new LogEntity()
                        {
                            AppName = AppName,
                            LogText = message + Environment.NewLine + "[Exception：]" + exp.ToString(),
                            LogTime = DateTime.Now,
                            LogType = (int)logType
                        };
                        CacheProvider.Set<LogEntity>(Guid.NewGuid().ToString(), Log, LogCacheSeconds, LogBucketName, LogBucketPassword);
                    }
                    else
                    {
                        File.AppendAllText(logFile, string.Format("{0} {1} {2}{3}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), logType.ToString(), message, Environment.NewLine));
                    }
                }
            }
        }
    }
}
